package NC8_二叉树中和为某一值的路径2;

import java.util.ArrayList;

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

}

}

public class Solution {
    ArrayList<ArrayList<Integer>> resultList = new ArrayList<>();
    ArrayList<Integer> list = new ArrayList<>();
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int expectNumber) {
        if (root == null){
            return resultList;
        }
        list.add(root.val);
        expectNumber -= root.val;

        if (expectNumber == 0 && root.left == null && root.right == null){
            resultList.add(new ArrayList<Integer>(list));
        }

        FindPath(root.left, expectNumber);
        FindPath(root.right, expectNumber);
        list.remove(list.size() - 1);   // add添加的是引用，如果不new一个的话，后面的操作会更改这个list
        return resultList;
    }
}